package cn.lirenyi.pay.service.impl;

import cn.lirenyi.basic.exception.BusinessException;
import cn.lirenyi.basic.service.impl.BaseServiceImpl;
import cn.lirenyi.order.service.IProductOrderService;
import cn.lirenyi.pay.domain.AlipayInfo;
import cn.lirenyi.pay.domain.PayBill;
import cn.lirenyi.pay.mapper.AlipayInfoMapper;
import cn.lirenyi.pay.mapper.PayBillMapper;
import cn.lirenyi.pay.service.IPayBillService;
import cn.lirenyi.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;


    @Override
    public String pay(PayBill billTmp) {
        if (billTmp == null){
            throw new BusinessException("我怀疑你在搞事情");
        }
        PayBill bill = payBillMapper.queryById(billTmp.getId());
        if (bill == null){
            throw new BusinessException("你滚啊，猪儿虫！");
        }
        // 判断是那种支付方式，0余额， 1支付宝，   2微信，    3银联
        switch (bill.getPayChannel().intValue()){
            case 1 ://支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2 ://微信

                break;
            case 3 ://银联

                break;
            default : //余额

        }

        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
